package com.jbit.examSystem.controller;

import com.alibaba.fastjson.JSON;
import com.jbit.examSystem.pojo.*;
import com.jbit.examSystem.service.StudentService;
import com.jbit.examSystem.service.TeacherService;
import com.jbit.examSystem.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("changePassword")
    public String changePassword(){
        return "index/changePassword";
    }

    @RequestMapping("changePwd")
    @ResponseBody
    public ResponseResult changePwd(User user, HttpSession session, HttpServletResponse resp){
        ResponseResult result = null;
        try {
            studentService.updatePwd(user);
            session.removeAttribute("emailCode");
            result = ResponseResult.success(null);
            Cookie cookie = new Cookie("password", URLEncoder.encode(user.getPassword(), "UTF-8"));
            cookie.setMaxAge(7*24*60*60);
            resp.addCookie(cookie);
        } catch (Exception e) {
            result = ResponseResult.error(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }
    @RequestMapping("oldPwdByNewPwd")
    @ResponseBody
    public Map<String, Object> oldPwdByNewPwd(Integer identityId,String oldPwd,String name){
        Map<String, Object> map = new HashMap<>();

        Boolean valid = studentService.verify(identityId,name, oldPwd);
        map.put("valid",valid);
        return map;
    }

    @RequestMapping("student/updateInfo")
    @ResponseBody
    public ResponseResult updateInfo(Student student,HttpSession session) {
        studentService.updateInfo(student);
        Student newStudent = studentService.findById(student.getId());
        session.setAttribute("user",newStudent);

        ResponseResult result = ResponseResult.success(null);

        return result;
    }

    @RequestMapping("teacher/updateInfo")
    @ResponseBody
    public ResponseResult updateInfo(User user,HttpSession session) {
        teacherService.updateInfo(user);
        Teacher teacher = teacherService.findById(user.getId());
        session.setAttribute("user",teacher);

        ResponseResult result = ResponseResult.success(null);

        return result;
    }

    @RequestMapping("teacher/findAll")
    @ResponseBody
    public ResponseResult findAll() {
        List<Teacher> teachers = teacherService.findAll();
        ResponseResult result = ResponseResult.success(teachers);

        return result;
    }
}
